<!-- 页面  -->
<template>
  <!--  <s-layout class="wallet-wrap" title="我的元宝" navbar="inner" :bg-style="{-->
  <!--    color:'#F5F5F5'-->
  <!--  }">-->
  <!--    <view-->
  <!--      class="header-box ss-flex ss-flex-col ss-row-center ss-col-center"-->
  <!--      :style="[-->
  <!--        {-->
  <!--          marginTop: '-' + Number(statusBarHeight + 88) + 'rpx',-->
  <!--          paddingTop: Number(statusBarHeight + 88) + 'rpx',-->
  <!--        },-->
  <!--      ]"-->
  <!--    >-->
  <!--      <view class="header-bg">-->
  <!--        <view class="bg"></view>-->
  <!--      </view>-->
  <!--      <view class="score-box ss-flex-col ss-row-center ss-col-center">-->
  <!--        <view class="ss-m-b-30">-->
  <!--          <text class="all-title ss-m-r-8">当前元宝</text>-->
  <!--          &lt;!&ndash; <text class="cicon-help-o"></text> &ndash;&gt;-->
  <!--        </view>-->
  <!--        <text class="all-num">{{ userInfo.score || 0 }}</text>-->
  <!--      </view>-->
  <!--      <view class="topUp flex align-center justify-center mt_30"-->
  <!--            @click.stop="goPath"-->
  <!--            v-if="mp_code != appVersion && state.data.status != 0"-->
  <!--      >-->
  <!--        充值-->
  <!--      </view>-->
  <!--      <view class="fs_20 color_F8F8F8 mt_15 fw_500"-->
  <!--            @click.stop="sheep.$router.go('/pages/public/richtext',{id:7,title:'元宝说明'})">元宝说明-->
  <!--      </view>-->
  <!--    </view>-->
  <!--    &lt;!&ndash; tab &ndash;&gt;-->
  <!--    <su-sticky :customNavHeight="sys_navBar">-->
  <!--      &lt;!&ndash; 统计 &ndash;&gt;-->
  <!--      <view class="filter-box ss-p-x-30 ss-flex ss-col-center ss-row-between">-->
  <!--        <uni-datetime-picker v-model="state.data" type="daterange" @change="onChangeTime" :end="state.today">-->
  <!--          <button class="ss-reset-button date-btn">-->
  <!--            <text>{{ dateFilterText }}</text>-->
  <!--            <text class="cicon-drop-down ss-seldate-icon"></text>-->
  <!--          </button>-->
  <!--        </uni-datetime-picker>-->

  <!--        <view class="total-box">-->
  <!--          <view class="ss-m-b-10">总收入￥{{ state.pagination.income }}</view>-->
  <!--          <view>总支出￥{{ -state.pagination.expense }}</view>-->
  <!--        </view>-->
  <!--      </view>-->
  <!--      <su-tabs-->
  <!--        :list="tabMaps"-->
  <!--        @change="onChange"-->
  <!--        :scrollable="false"-->
  <!--        :current="state.currentTab"-->
  <!--      ></su-tabs>-->
  <!--    </su-sticky>-->
  <!--    &lt;!&ndash; list &ndash;&gt;-->
  <!--    <view class="list-box">-->
  <!--      <view v-if="state.pagination.total > 0">-->
  <!--        <view-->
  <!--          class="list-item ss-flex ss-col-center ss-row-between"-->
  <!--          v-for="item in state.pagination.data"-->
  <!--          :key="item.id"-->
  <!--        >-->
  <!--          <view class="ss-flex-col">-->
  <!--            <view class="name">{{ item.event_text }}{{ item.memo ? '-' + item.memo : '' }}</view>-->
  <!--            <view class="time">{{ item.createtime }}</view>-->
  <!--          </view>-->
  <!--          <view class="add" v-if="item.amount > 0">+{{ parseInt(item.amount) }}</view>-->
  <!--          <view class="minus" v-else>{{ parseInt(item.amount) }}</view>-->
  <!--        </view>-->
  <!--      </view>-->
  <!--      <s-empty v-else text="暂无数据" icon="/static/data-empty.png" />-->
  <!--    </view>-->

  <!--    <uni-load-more-->
  <!--      v-if="state.pagination.total > 0"-->
  <!--      :status="state.loadStatus"-->
  <!--      :content-text="{-->
  <!--        contentdown: '上拉加载更多',-->
  <!--      }"-->
  <!--      @tap="onLoadMore"-->
  <!--    />-->
  <!--  </s-layout>-->
  <view class="wh_750 pageHeight flex align-center flex-column bg_F5F5F5">
    <s-header-title title="我的元宝"
                    is-display-back
                    is-fixed
                    show-f-icon
                    title-color="#FFFFFF"
                    :background="`rgba(246,88,73,${state.topColor / state.infoTop})`"
                    @getPageHeight="getPageHeight"
    ></s-header-title>
    <view class="header_bg wh_750 hW_402 flex flex-column align-center justify-end">
      <image :src="`${IMG_URL}/index/member_qustery.png`" class="wh_32 hW_32"
             style="position: absolute;top: 152rpx;right: 59rpx"
             @click.stop="sheep.$router.go('/pages/public/richtext',{id:7,title:'元宝说明'})"
      ></image>
      <view class="color_FFFFFF fs_60 fw_bold">{{ userInfo.score || 0 }}</view>
      <view class="fs_24 color_FFFFFF mt_5 mb_70">元宝余额</view>
    </view>
    <su-sticky id="infoMoney" class="hW_268 bg_FFFFFF flex flex-column align-center"
               style="border-radius: 20rpx;margin-top: -57rpx;"
               :style="{
                 top:state.headerHeight+ 'px',
                 width:`calc(702rpx + ${state.topColor > state.infoTop ? 48 : (state.topColor / state.infoTop) * 48}rpx)`,
                 borderRadius:`calc(20rpx - ${state.topColor > state.infoTop ? 20 : (state.topColor / state.infoTop) * 20}rpx)`
               }"
    >
      <view class="wh_660 ml_21 mr_21 flex align-center justify-between"
            style="border-bottom: 2rpx solid #EDEDED;padding: 30rpx 0"
      >
        <view class="flex align-start flex-column">
          <view class=" hW_50 fs_30 fw_500 flex align-center justify-center"
                style="border-radius: 25rpx;border:2rpx solid #707070;color: #6E6E6E;padding: 0 10rpx"
          >
            <uni-datetime-picker v-model="state.data" type="daterange" @change="onChangeTime" :end="state.today">
              <button class="ss-reset-button date-btn">
                <text>{{ dateFilterText }}</text>
                <text class="icon-ic_down2 iconfont fs_30"></text>
              </button>
            </uni-datetime-picker>
          </view>
          <view class="flex align-center fs_30 fw_500 color_9F9F9F mt_29">
            <view>
              总收入￥{{ state.pagination.income }}
            </view>
            <view class="ml_26">
              总支出￥{{ -state.pagination.expense }}
            </view>
          </view>
        </view>
        <view class="wh_158 hW_69 fs_27 color_FFFFFF fw_500 flex align-center justify-center"
              style="background: linear-gradient( 96deg, #FF4206 0%, #FF7950 100%);box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(0,0,0,0.16);border-radius: 35rpx;"
              @click.stop="goPath"
              v-if="mp_code != appVersion && state.data.status != 0"
        >
          元宝充值
        </view>
      </view>
      <view class="ml_21 mr_21 wh_660 flex align-center justify-around mt_10">
        <view v-for="(item,index) in tabMaps"
              :key="index"
              class="flex align-center flex-column"
              @click.stop="onChange({index})"
        >
          <view class="fs_30 fw_500"
                :style="{color: state.currentTab === index ? '#FF4206' : '#6E6E6E'}"
          >
            {{ item.name }}
          </view>
          <view class="icon-ic_down2 iconfont fs_30"
                :style="{color: state.currentTab === index ? '#FF4206' : 'transparent'}"
          ></view>
        </view>
      </view>
    </su-sticky>
    <view class="wh_702">
      <view v-if="state.pagination.total > 0">
        <view
          class="flex flex-column align-center"
          v-for="item in state.pagination.data"
          :key="item.id"
        >
          <view class="hW_20"></view>
          <view class="flex align-center justify-between wh_702 bg_FFFFFF"
                style="border-radius: 20rpx"
          >
            <view class="ss-flex-col ml_25" style="padding: 18rpx 0">
              <view class="fs_28 color_4B4B4B fw_500">{{ item.event_text }}{{ item.memo ? '-' + item.memo : '' }}</view>
              <view class="fs_22 color_9F9F9F fw_500">{{ item.createtime }}</view>
            </view>
            <view class="fs_36 fw_500 mr_25" style="color: #22DB00;" v-if="item.amount > 0">+{{ parseInt(item.amount)
              }}
            </view>
            <view class="fs_36 color_FF4206 fw_500 mr_25" v-else>{{ parseInt(item.amount) }}</view>
          </view>
        </view>
      </view>
      <s-empty v-else text="暂无数据" icon="/static/data-empty.png" />
    </view>
    <uni-load-more
      v-if="state.pagination.total > 0"
      :status="state.loadStatus"
      :content-text="{
            contentdown: '上拉加载更多',
          }"
      @tap="onLoadMore"
    />
  </view>
</template>

<script setup>
  import sheep from '@/sheep';
  import { onLoad, onReachBottom, onReady, onPageScroll } from '@dcloudio/uni-app';
  import { computed, reactive, nextTick, getCurrentInstance } from 'vue';
  import _ from 'lodash';
  import dayjs from 'dayjs';
  import SHeaderTitle from '@/sheep/components/s-header-title/s-header-title.vue';
  import { getPx } from '@/sheep/helper';

  const statusBarHeight = sheep.$platform.device.statusBarHeight * 2;
  const userInfo = computed(() => sheep.$store('user').userInfo);
  const sys_navBar = sheep.$platform.navbar;
  const mp_code = computed(() => sheep.$store('AI').mp_code);
  const appVersion = computed(() => sheep.$store('app').info.worship_version);
  const { proxy } = getCurrentInstance();

  const pagination = {
    data: [],
    current_page: 1,
    total: 1,
    last_page: 1,
    expense: 0,
    income: 0,
  };

  const state = reactive({
    currentTab: 0,
    pagination,
    loadStatus: '',
    date: [],
    data: {},
    today: '',
    infoTop: 0,
    topColor: 0,
    headerHeight: 0,
  });
  const IMG_URL = computed(() => sheep.$store('AI').sq_image_path);

  const tabMaps = [
    {
      name: '全部',
      value: 'all',
    },

    {
      name: '收入',
      value: 'income',
    },
    {
      name: '支出',
      value: 'expense',
    },
  ];

  const dateFilterText = computed(() => {
    if (state.date[0] === state.date[1]) {
      return state.date[0];
    } else {
      return state.date.join('~');
    }
  });

  async function getLogList(page = 1, list_rows = 8) {
    state.loadStatus = 'loading';
    let res = await sheep.$api.user.wallet.log({
      type: 'score',
      list_rows,
      page,
      tab: tabMaps[state.currentTab].value,
      date: appendTimeHMS(state.date),
    });
    if (res.code === 1) {
      let list = _.concat(state.pagination.data, res.data.list.data);
      state.pagination = {
        ...res.data.list,
        data: list,
        income: res.data.income,
        expense: res.data.expense,
      };
      if (state.pagination.current_page < state.pagination.last_page) {
        state.loadStatus = 'more';
      } else {
        state.loadStatus = 'noMore';
      }
    }
  }

  onLoad(async (options) => {
    state.today = dayjs().format('YYYY-MM-DD');
    state.date = [state.today, state.today];
    const res = await sheep.$api.trade.scoreRules();
    if (res.code === 1) {
      state.data = res.data;
      state.data.status = res.data.status;
      state.faceValueList = res.data.quick_amounts;
    }
    await getLogList();
  });

  onReady(() => {
    nextTick(() => {
      const query = uni.createSelectorQuery().in(proxy);
      query.select('#infoMoney').boundingClientRect(res => {
        state.infoTop = res.top;
      }).exec();
    });
  });

  onPageScroll((e) => {
    state.topColor = e.scrollTop;
  });

  function getPageHeight(number) {
    state.headerHeight = number;
  }

  function onChange(e) {
    state.pagination = pagination;
    state.currentTab = e.index;
    getLogList();
  }

  function onChangeTime(e) {
    state.date[0] = e[0];
    state.date[1] = e[e.length - 1];
    state.pagination = pagination;
    getLogList();
  }

  function appendTimeHMS(arr) {
    return [arr[0] + ' 00:00:00', arr[1] + ' 23:59:59'];
  }

  function onLoadMore() {
    if (state.loadStatus !== 'noMore') {
      getLogList(state.pagination.current_page + 1);
    }
  }

  function goPath() {
    sheep.$router.go('/pages/pay/recharge', { is_flag: 1 });
  }

  onReachBottom(() => {
    onLoadMore();
  });
</script>

<style lang="scss" scoped>
  .header-box {
    width: 100%;
    background: linear-gradient(180deg, var(--ui-BG-Main) 0%, var(--ui-BG-Main-gradient) 100%) no-repeat;
    background-size: 750rpx 100%;
    padding: 0 0 120rpx 0;
    box-sizing: border-box;

    .score-box {
      height: 100%;

      .all-num {
        font-size: 50rpx;
        font-weight: bold;
        color: #fff;
        font-family: OPPOSANS;
      }

      .all-title {
        font-size: 26rpx;
        font-weight: 500;
        color: #fff;
      }

      .cicon-help-o {
        color: #fff;
        font-size: 28rpx;
      }
    }
  }

  // 筛选
  .filter-box {
    height: 114rpx;
    background-color: $bg-page;

    .total-box {
      font-size: 24rpx;
      font-weight: 500;
      color: $dark-9;
    }

    .date-btn {
      background-color: $white;
      line-height: 54rpx;
      border-radius: 27rpx;
      padding: 0 20rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: $dark-6;

      .ss-seldate-icon {
        font-size: 50rpx;
        color: $dark-9;
      }
    }
  }

  .list-box {
    .list-item {
      background: #fff;
      border-bottom: 1rpx solid #dfdfdf;
      padding: 30rpx;

      .name {
        font-size: 28rpx;

        font-weight: 500;
        color: rgba(102, 102, 102, 1);
        line-height: 28rpx;
        margin-bottom: 20rpx;
      }

      .time {
        font-size: 24rpx;

        font-weight: 500;
        color: rgba(196, 196, 196, 1);
        line-height: 24px;
      }

      .add {
        font-size: 30rpx;

        font-weight: 500;
        color: #e6b873;
      }

      .minus {
        font-size: 30rpx;

        font-weight: 500;
        color: $dark-3;
      }
    }
  }

  .topUp {
    width: 120rpx;
    height: 50rpx;
    line-height: 60rpx;
    border-radius: 30px;
    font-size: 26rpx;
    font-weight: 500;
    background-color: #fff;
    color: var(--ui-BG-Main);
  }

  .header_bg {
    background: url($uni-bg-url-sq + '/index/yuanbao_bg_header.png') no-repeat;
    background-size: 100% 100%;
  }
</style>
